草庐IT

C++ : friend function in a template class for operator<<

全部标签

c++ - 为什么 uBLAS 没有 `operator*(matrix, vector)` ?

在doc,他们说Wedecidedtousenooperatoroverloadingfor...他们为这些提供了prod。但为什么?有什么好的理由吗?我喜欢做matrix*vector(和大多数其他语言一样)。我想了解为什么他们没有重载此运算符以了解为什么自己做可能是个坏主意。或者,如果我自己重载,它们不会有任何缺点吗? 最佳答案 可能是因为op*在其他语言中,例如使用Python中的Numpy,将始终是元素明智的。如果一个元素是矩阵而另一个元素是vector,它将尝试广播缺失维度中的所有元素。

c++ - std::map 是否要求比较器的 operator() 为常量?

在OSX10.8上使用libc++时,以下代码无法使用XCode4.5的clang++进行编译:#include#includeclassFoo{public:explicitFoo(intval_):val(val_){}intval;};structFooComparator{booloperator()(constFoo&left,constFoo&right){returnleft.valm;Foof(4);m[f]=std::string("four");return0;}错误:broken.cpp:11:8:note:candidatefunctionnotviable:'

c++ - Clang 无法识别 <algorithm> 中的 std::all_of

在我们的测试环境中编译时遇到了以下问题:尽管窗口已经在工作,但我们在Freebsd9上的构建失败并显示以下错误消息:error:nomembernamed'all_of'innamespace'std'鉴于我将-std=c++11添加到我们的Cmake标志中,我想知道为什么这不起作用。clangversion3.4(tags/RELEASE_34/final)Target:i386-portbld-freebsd9.1Threadmodel:posix函数如下#include...inlineboolis_positive_number(conststd::string&str){if

c++ - <function> 不是 <class> 的成员

我已将我的函数“Credit”声明为带有一些参数的私有(private)成员。我的观察是,每当我尝试不带任何参数进行编译时,编译器都会成功编译。但是一旦我用函数中的参数进行编译,编译器就会报错'Transaction::Credit'isnotamemberof'Transaction'这是我的代码classTransaction:publicMenu{private:voidCredit(intdepost);//{return0;}public:voidDeposit();voidWithdraw(){}voidTransfer(){}};voidTransaction::Depo

c++ - 为什么不使用强制转换语法调用 "operator void"?

在玩thisanswer时通过userGMan我制作了以下代码片段(使用VisualC++9编译):classClass{public:operatorvoid(){}};Classobject;static_cast(object);(void)object;object.operatorvoid();通过调试器后,我发现转换为void不会调用Class::operatorvoid(),只有第三次调用(显式调用运算符)实际上调用了运算符,这两个转换什么都不做。为什么operatorvoid没有用强制转换语法调用? 最佳答案 在§1

c++ - 为什么 g++ 声明某些 valarray<double> o 有 "no matching function for call cbegin(o)"?

请考虑以下代码:usingcustom_t=std::valarray;custom_to;unsignedacc=std::accumulate(std::cbegin(o),std::cend(o),0);g++-5说Nomatchingfunctionforcalltocbegin(custom_t&)如果我改用std::begin(o)和std::end(o),一切正常。这是编译器错误吗?代码使用VisualStudio2015编译。 最佳答案 这是一个libstdc++错误,我刚刚创建了https://gcc.gnu.or

c++ - "warning: operation of ... may be undefined"用于三元运算——不是 if/else block

这个问题在这里已经有了答案:Undefinedbehaviorandsequencepoints(5个答案)关闭6年前。这是我的代码:intmain(){staticinttest=0;constintanotherInt=1;test=anotherInt>test?test++:0;if(anotherInt>test)test++;elsetest=0;return0;}这是我构建它时产生的警告:../main.cpp:15:40:warning:operationon‘test’maybeundefined[-Wsequence-point]test=anotherInt>te

c++ - visual c++ <limits> 常量的多个定义

我有一个头文件和两个源文件。主要.cpp:#include#include"constant.h"intmain(){std::cout常量.h:#ifndefCONSTANT_H#defineCONSTANT_H#include#includeexternstd::unordered_mapconsttest;#endif常量.cpp:#include"constant.h"std::unordered_mapconsttest={{"Hello",1},{"World",2}};当我使用VisualC++(VisualStudio2015Update1)进行编译时,我遇到了很多多重

c++ - 从 boost::shared_ptr<string> 返回 C 字符串

我将一些C++代码包装在函数中,以便使C++方法在C中可用。C++API方法返回boost::shared_ptr通常的对象。我在C++中导出的函数如下所示:extern"C"constchar*Hazelcast_Map_get_int_string(Hazelcast_Client_t*hazelcastClient,constchar*mapName,intkey,char**errptr){IMapmap=hazelcastClient->client->getMap(mapName);boost::shared_ptrvalue=map.get(key);string*str

c++ - 为什么 std::shared_ptr 提供 operator<<?

std::shared_ptr提供operator它只是写出它的地址。没有operator>>只记录地址,不记录内容。我想知道它在哪些情况下有用。 最佳答案 因为是一个潜在有用的东西在原始指针上执行。这是安全的,原始指针就是这样做的,shared_ptr在某些情况下应该用于替换原始指针。相比之下,>>很少有意义。与原始指针不同,将指针值存储在共享指针中会取得它的所有权。我可以some_stream>>raw_ptr除非我用ptr做些什么没有任何问题;有点奇怪,但没有立即中断。对shared_ptr做同样的事情只有在极其深奥的情况下才